<?php

/**
 * This is the model class for table "karyawan".
 *
 * The followings are the available columns in table 'karyawan':
 * @property integer $id_kary
 * @property integer $id_type_dinas
 * @property integer $id_agama
 * @property integer $id_jabatan
 * @property string $nip
 * @property string $nama
 * @property string $tgl_lahir
 * @property string $tempat_lahir
 * @property string $jenis_kelamin
 * @property integer $stat_pernikahan
 * @property string $alamat
 * @property string $no_telp
 * @property string $npwp
 * @property string $pen_terakhir
 * @property string $tgl_mulai_kerja
 * @property string $tg_keluar_kerja
 *
 * The followings are the available model relations:
 * @property Agama $idAgama
 * @property Jabatan $idJabatan
 * @property TypeDinas $idTypeDinas
 * @property Tim[] $tims
 */
class Karyawan extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Karyawan the static model class
	 */
	public $agama,$nama_type_dinas,$nama_jabatan,$nama_golongan;
		
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'karyawan';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_type_dinas, id_agama, id_jabatan, nip, nama, tgl_lahir, tempat_lahir, jenis_kelamin, stat_pernikahan, alamat, no_telp, pen_terakhir', 'required'),
			array('id_type_dinas, id_agama, id_jabatan, stat_pernikahan,id_golongan', 'numerical', 'integerOnly'=>true),
			array('nip, nama, npwp, pen_terakhir', 'length', 'max'=>45),
			array('tempat_lahir, no_telp', 'length', 'max'=>20),
			array('jenis_kelamin', 'length', 'max'=>1),
			array('tgl_mulai_kerja, tg_keluar_kerja', 'safe'),
			array('nama_jabatan,agama,nama_type_dinas,nama_golongan', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_kary, id_type_dinas, id_agama, id_jabatan, nip, nama, tgl_lahir, tempat_lahir, jenis_kelamin, stat_pernikahan, alamat, no_telp, npwp, pen_terakhir, tgl_mulai_kerja, tg_keluar_kerja', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idAgama' => array(self::BELONGS_TO, 'Agama', 'id_agama'),
			'idJabatan' => array(self::BELONGS_TO, 'Jabatan', 'id_jabatan'),
			'idTypeDinas' => array(self::BELONGS_TO, 'TypeDinas', 'id_type_dinas'),
			'idGolongan' => array(self::BELONGS_TO, 'Golongan', 'id_golongan'),
			'tims' => array(self::HAS_MANY, 'Tim', 'id_kary'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_kary' => 'Id Kary',
			'id_type_dinas' => 'Type Dinas',
			'id_agama' => 'Agama',
			'id_golongan' => 'Golongan',
			'id_jabatan' => 'Jabatan',
			'nip' => 'Nip',
			'nama' => 'Nama',
			'tgl_lahir' => 'Tgl Lahir',
			'tempat_lahir' => 'Tempat Lahir',
			'jenis_kelamin' => 'Jenis Kelamin',
			'stat_pernikahan' => 'Status Pernikahan',
			'alamat' => 'Alamat',
			'no_telp' => 'No Telp',
			'npwp' => 'NPWP',
			'pen_terakhir' => 'Pendidikan Trkhir',
			'tgl_mulai_kerja' => 'Tgl Mulai Kerja',
			'tg_keluar_kerja' => 'Tg Keluar Kerja',
			'nama_jabatan' => 'Jabatan',
			'agama' => 'Agama',
			'nama_type_dinas' => 'Type Dinas',
			'nama_golongan' => 'Golongan',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->alias = 'k';
        $criteria->select = 'k.*,a.agama,j.nama_jabatan,ty.nama_type_dinas,g.nama_golongan'; 
		$criteria->join = ' left join type_dinas ty on ty.id_type_dinas = k.id_type_dinas';
		$criteria->join .= ' left join agama a on a.id_agama = k.id_agama';
		$criteria->join .= ' left join jabatan j on j.id_jabatan = k.id_jabatan';
		$criteria->join .= ' left join golongan g on g.id_golongan = k.id_golongan';
		
		$criteria->compare('id_kary',$this->id_kary);
		$criteria->compare('id_type_dinas',$this->id_type_dinas);
		$criteria->compare('id_agama',$this->id_agama);
		$criteria->compare('id_jabatan',$this->id_jabatan);
		$criteria->compare('nip',$this->nip,true);
		$criteria->compare('nama',$this->nama,true);
		$criteria->compare('tgl_lahir',$this->tgl_lahir,true);
		$criteria->compare('tempat_lahir',$this->tempat_lahir,true);
		$criteria->compare('jenis_kelamin',$this->jenis_kelamin,true);
		$criteria->compare('stat_pernikahan',$this->stat_pernikahan);
		$criteria->compare('alamat',$this->alamat,true);
		$criteria->compare('no_telp',$this->no_telp,true);
		$criteria->compare('npwp',$this->npwp,true);
		$criteria->compare('pen_terakhir',$this->pen_terakhir,true);
		$criteria->compare('tgl_mulai_kerja',$this->tgl_mulai_kerja,true);
		$criteria->compare('tg_keluar_kerja',$this->tg_keluar_kerja,true);
		$criteria->compare('agama',$this->agama,true);
		$criteria->compare('nama_jabatan',$this->nama_jabatan,true);
		$criteria->compare('nama_type_dinas',$this->nama_type_dinas,true);
		$criteria->compare('nama_golongan',$this->nama_golongan,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				  'attributes'=>array(
						 'agama'=>array(
							 'asc'=>'agama',
							 'desc'=>'agama DESC',
						  ),
						  'nama_jabatan'=>array(
							 'asc'=>'nama_jabatan',
							 'desc'=>'nama_jabatan DESC',
						  ),
						  'nama_golongan'=>array(
							 'asc'=>'nama_golongan',
							 'desc'=>'nama_golongan DESC',
						  ),
						  'nama_type_dinas'=>array(
							 'asc'=>'nama_type_dinas',
							 'desc'=>'nama_type_dinas DESC',
						  ),
						  'nip'=>array(
							 'asc'=>'nip',
							 'desc'=>'nip DESC',
						  ),
						  'nama'=>array(
							 'asc'=>'nama',
							 'desc'=>'nama DESC',
						  ),
				  )
			  ),
                    'pagination' => array(
							'pageSize' => 5,
						),
		));
	}
}